//
//  PKReadingTableViewControllerCell.m
//  PianKe
//
//  Created by mingtao on 15/10/12.
//  Copyright (c) 2015年 mingtao. All rights reserved.
//

#import "PKReadingTableViewControllerCell.h"
#import "PKRequestManager.h"

#import "PKReadingTableViewCell.h"
#import "PKReadinglistTableViewController2.h"

#import "PKReadingDetailViewController.h"

@interface PKReadingTableViewControllerCell ()<UITableViewDataSource,UITableViewDelegate>

@property(nonatomic,strong)NSMutableArray *listArray;

@property(nonatomic,strong)UITableView *mainTableView;//主页面

@property(nonatomic,strong)UIView *contenview;


@property(nonatomic,assign)BOOL isbool;

@property(nonatomic,strong)UIScrollView *Scrollview;

@property(nonatomic,strong)PKReadinglistTableViewController2 *table2;

@property(nonatomic,strong)UIButton *butNEW;

@property(nonatomic,strong)UIButton *butHOT;

@end

@implementation PKReadingTableViewControllerCell



-(NSMutableArray *)listArray
{
    if (_listArray ==nil) {
        _listArray = [NSMutableArray arrayWithCapacity:0];
    }
    return _listArray;

}


-(void)requestAll:(NSString *)str;
{

        NSMutableDictionary *dictReading=[NSMutableDictionary dictionary];
    
    NSLog(@"%@",_listModel.type);
    if (self.strID !=nil) {
        dictReading[@"typeid"] =self.strID;
        dictReading[@"deviceid"] = @"63A94D37-33F9-40FF-9EBB-481182338873";
        dictReading[@"client"] = @"1";
        dictReading[@"sort"] = str;
    }else
    {
        dictReading[@"typeid"] =[NSString stringWithFormat:@"%@",_listModel.type];
        dictReading[@"deviceid"] = @"63A94D37-33F9-40FF-9EBB-481182338873";
        dictReading[@"client"] = @"1";
        dictReading[@"sort"] = str;
    
    }
    

    NSLog(@"%@",[NSString stringWithFormat:@"%@",self.listModel.type]);
        [PKRequestManager requestWithType:POST urlString:@"http://api2.pianke.me/read/columns_detail" parDic:dictReading finish:^(NSData *data) {
            
            NSDictionary *dicData=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
            NSArray *array=dicData[@"data"][@"list"];
            
            
            for (NSDictionary *keyDic in array) {
                
                PKRadingListModel *listModel=[[PKRadingListModel alloc]init];
                [listModel setValuesForKeysWithDictionary:keyDic];
                
                [self.listArray addObject:listModel];
                
            }
            
            [_mainTableView reloadData];

            
        } error:^(NSError *error) {
            NSLog(@"PKReadingTableViewControllerCell 请求异常%@",error);
        }];

}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    UIView *vie=[[UIView alloc]initWithFrame:CGRectMake(0, 20, WIHES, 44)];
    vie.backgroundColor = [UIColor whiteColor];
    
    UILabel *lable=[[UILabel alloc]initWithFrame:CGRectMake(100, 20, 100, 30)];
    lable.text =_listModel.name;
    
    UIButton *butImageT=[UIButton buttonWithType:(UIButtonTypeCustom)];
    butImageT.frame = CGRectMake(0, 20, 80, 30);
    
    [butImageT setBackgroundImage:[UIImage imageNamed:@"iconfont-jiantouicon"] forState:(UIControlStateNormal)];
    
    [butImageT addTarget:self action:@selector(fanhui:) forControlEvents:(UIControlEventTouchUpInside)];
    _butHOT =[UIButton buttonWithType:(UIButtonTypeCustom)];
    _butHOT.frame = CGRectMake(240, 20, 50, 30);

    [_butHOT setTitle:@"HOT" forState:(UIControlStateNormal)];
    
    [_butHOT setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
    [_butHOT addTarget:self action:@selector(butHOTCOLI:) forControlEvents:(UIControlEventTouchUpInside)];
    
    
    _butNEW=[UIButton buttonWithType:(UIButtonTypeCustom)];
    _butNEW.frame = CGRectMake(320, 20, 50, 30);
    [_butNEW setTitle:@"NEW" forState:(UIControlStateNormal)];
    [_butNEW setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
    
    [_butNEW addTarget:self action:@selector(butlick:) forControlEvents:(UIControlEventTouchUpInside)];
    
    
    [vie addSubview:_butHOT];
    [vie addSubview:_butNEW];
    [vie addSubview:butImageT];
    [vie addSubview:lable];
    [self.view addSubview:vie];
    
    _Scrollview=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 70, WIHES, HHIHEI-70)];
    _mainTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, WIHES, HHIHEI)];
    
    _Scrollview.contentSize = CGSizeMake(WIHES*2, 0);
    
    _Scrollview.backgroundColor = [UIColor whiteColor];
    _Scrollview.pagingEnabled = YES;
    _Scrollview.contentOffset = CGPointMake(0, 0);
    
    
    _mainTableView.dataSource = self;
    _mainTableView.delegate = self;
    _mainTableView.rowHeight=140;
    
    [_Scrollview addSubview:_mainTableView];
    _table2=[[PKReadinglistTableViewController2 alloc]init];
    
    if (self.strID!=nil) {
        _table2.strID=self.strID;
    }else
    {
    _table2.strID = [NSString stringWithFormat:@"%@",self.listModel.type];
    }
    _table2.tableView.frame=CGRectMake(WIHES, 0, WIHES, HHIHEI);
    [_Scrollview addSubview:_table2.tableView];
    [self.view addSubview:_Scrollview];

    
    [_mainTableView registerClass:[PKReadingTableViewCell class] forCellReuseIdentifier:@"PKReadingTableViewCell"];

    //请求数据
    [self requestAll:@"hot"];
    

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

//- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
//#warning Potentially incomplete method implementation.
//    // Return the number of sections.
//    return 1;
//}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSLog(@"%lu",self.listArray.count);

    return self.listArray.count;



}
//- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
//#warning Incomplete method implementation.
//    // Return the number of rows in the section.
//
//}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    PKReadingTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"PKReadingTableViewCell" forIndexPath:indexPath];


        cell.listPP = _listArray[indexPath.row];
    


    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    PKReadingDetailViewController *web=[[PKReadingDetailViewController alloc]init];
        PKRadingListModel *listModel=self.listArray[indexPath.row];
    
    web.strtype=listModel.ID;
    
    

    
    [self.navigationController pushViewController:web animated:YES];


}


/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
-(void)butlick:(UIButton *)but
{
    [_table2.tableView reloadData];
    _Scrollview.contentOffset = CGPointMake(WIHES, 0);
    _butHOT.backgroundColor = [UIColor whiteColor];
    _butNEW.backgroundColor =[UIColor colorWithRed:0.158 green:1.000 blue:0.786 alpha:0.500];
}

-(void)butHOTCOLI:(UIButton *)but
{
    _Scrollview.contentOffset = CGPointMake(0, 0);

    _butNEW.backgroundColor = [UIColor whiteColor];
    _butHOT.backgroundColor = [UIColor colorWithRed:0.158 green:1.000 blue:0.786 alpha:0.500];

}

-(void)fanhui:(UIButton *)but
{

    [self.navigationController popToRootViewControllerAnimated:YES];
}

@end
